package com.longma.server.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longma.server.constant.enums.ResponseCode;
import com.longma.server.dao.InspectionRecordDAO;
import com.longma.server.entity.InspectionRecord;
import com.longma.server.exception.BusinessException;
import com.longma.server.pojo.dto.*;
import com.longma.server.service.IInspectionRecordService;
import com.longma.server.util.MybatisPlusPageUtil;
import com.longma.server.util.WrappersUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@Service
@Transactional
public class InspectionRecordServiceImpl implements IInspectionRecordService {

    @Autowired
    InspectionRecordDAO inspectionRecordDAO;

    /**
     * 新增巡視记录
     */
    @Override
    public InspectionRecord addInspectionRecord(InspectionRecordAddDTO addRequestDTO) {
        InspectionRecord inspectionRecord = new InspectionRecord();
        BeanUtil.copyProperties(addRequestDTO,inspectionRecord);
        inspectionRecordDAO.save(inspectionRecord);
        return inspectionRecord;
    }
    /**
     * 根据巡視記錄id删除巡視记录
     */
    @Override
    public boolean deleteInspectionRecordById(Integer id) {
        if (!isExistId(id)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }
        return inspectionRecordDAO.removeById(id);
    }
    /**
     * 根据巡視记录id列表删除巡視记录
     */
    @Override
    public boolean deleteInspectionRecordByIds(List<Integer> ids) {
        if (!isExistIds(ids)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        return inspectionRecordDAO.removeByIds(ids);
    }
    /**
     * 分页查询 获取巡視记录
     */
    @Override
    public IPage<InspectionRecord> getInspectionRecordByPage(Long pageNum, Long pageSize, Boolean all, List<AdvanceQueryConditionDTO> conditions) {
        LambdaQueryWrapper<InspectionRecord> wrapper = WrappersUtil.lambdaQuery(conditions, InspectionRecord.class);
        Page<InspectionRecord> pageRecord = MybatisPlusPageUtil.getPage(pageNum, pageSize, all);
        return inspectionRecordDAO.page(pageRecord, wrapper);
    }
    /**
     * 更新巡視记录
     */
    @Override
    public InspectionRecord updateInspectionRecord(InspectionRecordUpdateDTO updateRequestDTO){

        // 存在校验
        if(inspectionRecordDAO.getById(updateRequestDTO.getId()) == null){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        // 复制
        InspectionRecord inspectionRecord = new InspectionRecord();
        BeanUtil.copyProperties(updateRequestDTO,inspectionRecord);
        inspectionRecordDAO.updateById(inspectionRecord);
        return inspectionRecord;
    }
    /**
     * 判断id列表是否存在
     */
    private boolean isExistIds(List<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return false;
        }
        LambdaQueryWrapper<InspectionRecord> wrapper = Wrappers.lambdaQuery();
        Set<Integer> productIds = new HashSet<>(ids);
        return inspectionRecordDAO.count(wrapper.in(InspectionRecord::getId, productIds)) == productIds.size();
    }

    /**
     * 判断id是否存在
     */
    private boolean isExistId(Integer id) {
        return isExistIds(Arrays.asList(id));
    }
}
